package com.yingpai.fitness.dvp_2_0.main.fragment.mine.shop;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.yingpai.fitness.R;
import com.yingpai.fitness.base.BaseResponse;
import com.yingpai.fitness.dvp_2_0.common.adapter.BaseAdapterHelper;
import com.yingpai.fitness.dvp_2_0.common.adapter.QuickAdapter;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.fragment.CommonFragment;
import com.yingpai.fitness.dvp_2_0.common.util.GsonUtil;
import com.yingpai.fitness.dvp_2_0.common.util.SharedPreferencesHelp;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.common.view.XListView;
import com.yingpai.fitness.dvp_2_0.main.activity.mine.CommentActivity;
import com.yingpai.fitness.dvp_2_0.main.activity.mine.LogisticsActivity;
import com.yingpai.fitness.dvp_2_0.main.activity.mine.shop_refund.RefundServiceActivity;
import com.yingpai.fitness.dvp_2_0.main.activity.shop.ShopDetailActivity;
import com.yingpai.fitness.dvp_2_0.main.bean.pay.ChongZhiDingDanEntiy;
import com.yingpai.fitness.dvp_2_0.main.bean.pay.ZfbChongZhiEntity;
import com.yingpai.fitness.dvp_2_0.main.callback.WechatCallBack;
import com.yingpai.fitness.dvp_2_0.main.util.DialogUtil;
import com.yingpai.fitness.dvp_2_0.main.util.PayUtil;
import com.yingpai.fitness.dvp_2_0.main.util.WechatLoginUtil;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;
import com.yingpai.fitness.dvp_2_0.main.widget.round.RCRelativeLayout;
import com.yingpai.fitness.entity.OrderListBean;
import com.yingpai.fitness.entity.ShopDetailMcipChildBean;
import com.yingpai.fitness.entity.wx.TokenBean;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by ASUS on 2018/11/23.
 */

public class OrderListFragment extends CommonFragment implements OnRefreshLoadMoreListener, PayUtil.codeResultClickInterface, WechatCallBack {

    @BindView(R.id.listview)
    ListView listView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    Unbinder unbinder;
    private String status;
    private QuickAdapter<OrderListBean.MapBean.PageInfoBean.ListBean> quickAdapter;
    private TokenBean tokenBean;
    private int pageNum = 1;
    private int pageSize = 10;
    private String tag = "";
    private int orderId;
    private DecimalFormat df = new DecimalFormat("######.###");

    public static OrderListFragment newInstance(String status) {
        OrderListFragment orderListFragment = new OrderListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("status", status);
        orderListFragment.setArguments(bundle);
        return orderListFragment;
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dvp_2_0_fragment_order_list, container, false);
        unbinder = ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        refreshLayout.setOnRefreshLoadMoreListener(this);
        refreshLayout.setRefreshHeader(new MaterialHeader(context).setColorSchemeResources(R.color.common_color));
        tokenBean = (TokenBean) SharedPreferencesHelp.getBean(Constant.TOKENBEAN);
        status = getArguments().getString("status");

        quickAdapter = new QuickAdapter<OrderListBean.MapBean.PageInfoBean.ListBean>(context, R.layout.item_order_list) {
            @Override
            protected void convert(final BaseAdapterHelper helper, final OrderListBean.MapBean.PageInfoBean.ListBean pItem) {
//                helper.setText(R.id.title,);
                helper.setText(R.id.order, "订单号：" + pItem.getOrderCode());
                int shopCount = 0;
                for (int i = 0; i < pItem.getDetailList().size(); i++) {
                    shopCount = shopCount + pItem.getDetailList().get(i).getPurchaseAmount();
                }
                helper.setText(R.id.shop_count, "共" + shopCount + "件商品");
                helper.setText(R.id.money, "￥" + df.format(pItem.getTotalMerchandisePrice() / 100));

                XListView child_listview = helper.getView(R.id.child_listview);

                if (pItem.getStatus().equals("0")) {
                    TextView right_tv = helper.getView(R.id.right_tv);
                    TextView left_tv = helper.getView(R.id.left_tv);
                    right_tv.setText("立即付款");
                    left_tv.setText("取消订单");
                    right_tv.setVisibility(View.VISIBLE);
                    left_tv.setVisibility(View.VISIBLE);
                    QuickAdapter<OrderListBean.MapBean.PageInfoBean.ListBean.DetailListBean> child_adapter =
                            new QuickAdapter<OrderListBean.MapBean.PageInfoBean.ListBean.DetailListBean>(OrderListFragment.this.context, R.layout.item_child_order_list, pItem.getDetailList()) {
                                @Override
                                protected void convert(BaseAdapterHelper helper, final OrderListBean.MapBean.PageInfoBean.ListBean.DetailListBean item) {
                                    helper.getView(R.id.item_rl).setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            Bundle bundle = new Bundle();
                                            bundle.putInt("id", item.getMerchandiseId());
                                            startActivity(ShopDetailActivity.class, bundle);
                                        }
                                    });
                                    helper.setText(R.id.title, item.getMerchandiseName());
                                    helper.setText(R.id.money, "￥" + df.format(item.getSinglePrice() / 100));
                                    List<ShopDetailMcipChildBean> mcipList = GsonUtil.jsonToList(item.getMcipValue(), ShopDetailMcipChildBean.class);
                                    StringBuilder stringBuilder = new StringBuilder();
                                    for (int i = 0; i < mcipList.size(); i++) {
                                        if (i == mcipList.size() - 1) {
                                            stringBuilder.append(mcipList.get(i).getPropertyValue());
                                        } else {
                                            stringBuilder.append(mcipList.get(i).getPropertyValue() + ";");
                                        }
                                    }
                                    helper.setText(R.id.num_spec, "数量：x" + item.getPurchaseAmount() + ";" + stringBuilder.toString());
                                    RCRelativeLayout rc_layout = helper.getView(R.id.rc_layout);
                                    ImageView photo = helper.getView(R.id.photo);
                                    Glide.with(OrderListFragment.this.context)
                                            .load(item.getPhotoUrl())
                                            .error(R.mipmap.default_small_bg)
                                            .into(photo);
                                    rc_layout.setTopLeftRadius(6);
                                    rc_layout.setTopRightRadius(6);
                                    rc_layout.setBottomLeftRadius(6);
                                    rc_layout.setBottomRightRadius(6);
                                }
                            };
                    child_listview.setAdapter(child_adapter);

                    right_tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            DialogUtil.getInstance().showBottomDialog(OrderListFragment.this.context, R.layout.bottom_pay_layout);
                            View dialog_layout = DialogUtil.getInstance().dialog_bottom_layout;
                            dialog_layout.findViewById(R.id.outImg).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    DialogUtil.getInstance().dialog_bottom.dismiss();
                                }
                            });

                            TextView payPriceTv = dialog_layout.findViewById(R.id.payPriceTv);
                            payPriceTv.setText("支付 ￥" + df.format((pItem.getTotalMerchandisePrice() + pItem.getTotalPostPrice()) / 100));

                            final CheckBox mWeChatPayCb = dialog_layout.findViewById(R.id.weChatPayCb);
                            final CheckBox mALiPayCb = dialog_layout.findViewById(R.id.aLiPayCb);

                            mWeChatPayCb.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (mWeChatPayCb.isChecked()) {
                                        tag = Constant.WECHAT;
                                        mALiPayCb.setChecked(false);
                                    } else {
                                        tag = "";
                                    }
                                }
                            });
                            mALiPayCb.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (mALiPayCb.isChecked()) {
                                        tag = Constant.ALIPAY;
                                        mWeChatPayCb.setChecked(false);
                                    } else {
                                        tag = "";
                                    }
                                }
                            });

                            Button goPayBtn = dialog_layout.findViewById(R.id.goPayBtn);

                            goPayBtn.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (tag.equals(Constant.ALIPAY)) {
                                        orderId = pItem.getOrderId();
                                        showDialog(OrderListFragment.this.context, "支付中...");
                                        HttpParams httpParams = new HttpParams();
                                        httpParams.put("id", pItem.getOrderId());
                                        httpParams.put("customerId", tokenBean.getId());
                                        finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.SHOP_ALI_PAY, httpParams, Url.SHOP_ALI_PAY, new Callback<ZfbChongZhiEntity>() {
                                            @Override
                                            public void onSuccess(ZfbChongZhiEntity zfbChongZhiEntity) {
                                                if (zfbChongZhiEntity.getResult().equals(Constant.REQUEST_SUCCESS)) {
                                                    PayUtil.zfbpay(OrderListFragment.this, OrderListFragment.this.context, zfbChongZhiEntity.getMap().getResponse());
                                                } else {
                                                    showToast(zfbChongZhiEntity.getMsg() + "");
                                                }
                                                DialogUtil.getInstance().dialog_bottom.dismiss();
                                            }
                                        });
                                    } else if (tag.equals(Constant.WECHAT)) {
                                        orderId = pItem.getOrderId();
                                        showDialog(OrderListFragment.this.context, "支付中...");
                                        HttpParams httpParams = new HttpParams();
                                        httpParams.put("id", pItem.getOrderId());
                                        httpParams.put("customerId", tokenBean.getId());
                                        finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.SHOP_WECHAT_PAY, httpParams, Url.SHOP_WECHAT_PAY, new Callback<ChongZhiDingDanEntiy>() {
                                            @Override
                                            public void onSuccess(ChongZhiDingDanEntiy chongZhiDingDanEntiy) {
                                                if (chongZhiDingDanEntiy.getResult().equals(Constant.REQUEST_SUCCESS)) {
                                                    WechatLoginUtil.getInstance().setWechatCallBack(OrderListFragment.this);
                                                    PayUtil.wxpay(OrderListFragment.this.context, chongZhiDingDanEntiy.getMap().getResponse().getAppid(),
                                                            chongZhiDingDanEntiy.getMap().getResponse().getPartnerid(),
                                                            chongZhiDingDanEntiy.getMap().getResponse().getPrepayid(),
                                                            chongZhiDingDanEntiy.getMap().getResponse().getNoncestr(),
                                                            chongZhiDingDanEntiy.getMap().getResponse().getTimestamp() + "",
                                                            chongZhiDingDanEntiy.getMap().getResponse().getSign());
                                                } else {
                                                    showToast(chongZhiDingDanEntiy.getMsg() + "");
                                                }
                                                DialogUtil.getInstance().dialog_bottom.dismiss();
                                            }
                                        });
                                    } else {
                                        showToast("请选择支付方式");
                                    }

                                }
                            });

                        }
                    });

                    left_tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            DialogUtil.getInstance().showCenterDialog(OrderListFragment.this.context, R.layout.dialog_shop_pay_success);
                            View dialog_layout = DialogUtil.getInstance().dialog_center_layout;

                            TextView title = dialog_layout.findViewById(R.id.title);
                            title.setText("确定取消订单？");
                            TextView message = dialog_layout.findViewById(R.id.message);
                            message.setVisibility(View.GONE);
                            dialog_layout.findViewById(R.id.sure_cancel).setVisibility(View.VISIBLE);

                            dialog_layout.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    DialogUtil.getInstance().dialog_center.dismiss();
                                }
                            });

                            dialog_layout.findViewById(R.id.sure).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    DialogUtil.getInstance().dialog_center.dismiss();
                                    showDialog(OrderListFragment.this.context, null);
                                    HttpParams httpParams = new HttpParams();
                                    httpParams.put("id", pItem.getOrderId());
                                    finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.CANCEL_ORDER, httpParams, Url.CANCEL_ORDER, new Callback<BaseResponse>() {
                                        @Override
                                        public void onSuccess(BaseResponse baseResponse) {
                                            dismissDialog();
                                            if (baseResponse.getResult().equals(Constant.REQUEST_SUCCESS)) {
//                                                onRefresh(refreshLayout);
//                                                quickAdapter.remove(helper.getPosition());
//                                                quickAdapter.notifyDataSetChanged();
                                                EventBus.getDefault().post("0");
                                            } else {
                                                showToast(baseResponse.getMsg());
                                            }
                                        }
                                    });
                                }
                            });
                            dialog_layout.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    DialogUtil.getInstance().dialog_center.dismiss();
                                }
                            });
                        }
                    });
                } else if (pItem.getStatus().equals("1") || pItem.getStatus().equals("7")) {
                    TextView right_tv = helper.getView(R.id.right_tv);
                    TextView left_tv = helper.getView(R.id.left_tv);
                    right_tv.setText("提醒发货");
                    right_tv.setVisibility(View.VISIBLE);
                    left_tv.setVisibility(View.GONE);
                    QuickAdapter<OrderListBean.MapBean.PageInfoBean.ListBean.DetailListBean> child_adapter =
                            new QuickAdapter<OrderListBean.MapBean.PageInfoBean.ListBean.DetailListBean>(OrderListFragment.this.context, R.layout.item_child_order_list, pItem.getDetailList()) {
                                @Override
                                protected void convert(BaseAdapterHelper helper, final OrderListBean.MapBean.PageInfoBean.ListBean.DetailListBean item) {
                                    helper.getView(R.id.item_rl).setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            Bundle bundle = new Bundle();
                                            bundle.putInt("id", item.getMerchandiseId());
                                            startActivity(ShopDetailActivity.class, bundle);
                                        }
                                    });
                                    TextView return_money_goods = helper.getView(R.id.return_money_goods);
                                    return_money_goods.setVisibility(View.VISIBLE);
                                    return_money_goods.setText("退款");

                                    helper.setText(R.id.title, item.getMerchandiseName());
                                    helper.setText(R.id.money, "￥" + df.format(item.getSinglePrice() / 100));
                                    List<ShopDetailMcipChildBean> mcipList = GsonUtil.jsonToList(item.getMcipValue(), ShopDetailMcipChildBean.class);
                                    StringBuilder stringBuilder = new StringBuilder();
                                    for (int i = 0; i < mcipList.size(); i++) {
                                        if (i == mcipList.size() - 1) {
                                            stringBuilder.append(mcipList.get(i).getPropertyValue());
                                        } else {
                                            stringBuilder.append(mcipList.get(i).getPropertyValue() + ";");
                                        }
                                    }
                                    helper.setText(R.id.num_spec, "数量：x" + item.getPurchaseAmount() + ";" + stringBuilder.toString());
                                    RCRelativeLayout rc_layout = helper.getView(R.id.rc_layout);
                                    ImageView photo = helper.getView(R.id.photo);
                                    Glide.with(OrderListFragment.this.context)
                                            .load(item.getPhotoUrl())
                                            .error(R.mipmap.default_small_bg)
                                            .into(photo);
                                    rc_layout.setTopLeftRadius(6);
                                    rc_layout.setTopRightRadius(6);
                                    rc_layout.setBottomLeftRadius(6);
                                    rc_layout.setBottomRightRadius(6);

                                    return_money_goods.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            Bundle bundle = new Bundle();
                                            bundle.putString("tag", "退款");
                                            bundle.putInt("orderId", pItem.getOrderId());
                                            bundle.putSerializable("DetailListBean", item);
                                            startActivity(RefundServiceActivity.class, bundle);
                                        }
                                    });
                                }
                            };
                    child_listview.setAdapter(child_adapter);

                    right_tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            showToast("提醒成功");
                        }
                    });
                } else if (pItem.getStatus().equals("2")) {
                    TextView right_tv = helper.getView(R.id.right_tv);
                    TextView left_tv = helper.getView(R.id.left_tv);
                    right_tv.setText("确认收货");
                    left_tv.setText("查看物流");
                    right_tv.setVisibility(View.VISIBLE);
                    left_tv.setVisibility(View.VISIBLE);
                    QuickAdapter<OrderListBean.MapBean.PageInfoBean.ListBean.DetailListBean> child_adapter =
                            new QuickAdapter<OrderListBean.MapBean.PageInfoBean.ListBean.DetailListBean>(OrderListFragment.this.context, R.layout.item_child_order_list, pItem.getDetailList()) {
                                @Override
                                protected void convert(BaseAdapterHelper helper, final OrderListBean.MapBean.PageInfoBean.ListBean.DetailListBean item) {
                                    helper.getView(R.id.item_rl).setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            Bundle bundle = new Bundle();
                                            bundle.putInt("id", item.getMerchandiseId());
                                            startActivity(ShopDetailActivity.class, bundle);
                                        }
                                    });
                                    helper.setText(R.id.title, item.getMerchandiseName());
                                    helper.setText(R.id.money, "￥" + df.format(item.getSinglePrice() / 100));
                                    List<ShopDetailMcipChildBean> mcipList = GsonUtil.jsonToList(item.getMcipValue(), ShopDetailMcipChildBean.class);
                                    StringBuilder stringBuilder = new StringBuilder();
                                    for (int i = 0; i < mcipList.size(); i++) {
                                        if (i == mcipList.size() - 1) {
                                            stringBuilder.append(mcipList.get(i).getPropertyValue());
                                        } else {
                                            stringBuilder.append(mcipList.get(i).getPropertyValue() + ";");
                                        }
                                    }
                                    helper.setText(R.id.num_spec, "数量：x" + item.getPurchaseAmount() + ";" + stringBuilder.toString());
                                    RCRelativeLayout rc_layout = helper.getView(R.id.rc_layout);
                                    ImageView photo = helper.getView(R.id.photo);
                                    Glide.with(OrderListFragment.this.context)
                                            .load(item.getPhotoUrl())
                                            .error(R.mipmap.default_small_bg)
                                            .into(photo);
                                    rc_layout.setTopLeftRadius(6);
                                    rc_layout.setTopRightRadius(6);
                                    rc_layout.setBottomLeftRadius(6);
                                    rc_layout.setBottomRightRadius(6);
                                }
                            };
                    child_listview.setAdapter(child_adapter);

                    left_tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Bundle bundle = new Bundle();
                            bundle.putString("com", pItem.getExpressCompanyCode());
                            bundle.putString("num", pItem.getExpressNumber());
                            bundle.putString("state", pItem.getStatus());
                            startActivity(LogisticsActivity.class, bundle);
                        }
                    });

                    right_tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            DialogUtil.getInstance().showCenterDialog(OrderListFragment.this.context, R.layout.dialog_shop_pay_success);
                            View dialog_layout = DialogUtil.getInstance().dialog_center_layout;

                            TextView title = dialog_layout.findViewById(R.id.title);
                            title.setText("确定已收到商品？");
                            TextView message = dialog_layout.findViewById(R.id.message);
                            message.setVisibility(View.GONE);
                            dialog_layout.findViewById(R.id.sure_cancel).setVisibility(View.VISIBLE);

                            dialog_layout.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    DialogUtil.getInstance().dialog_center.dismiss();
                                }
                            });

                            dialog_layout.findViewById(R.id.sure).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    DialogUtil.getInstance().dialog_center.dismiss();
                                    showDialog(OrderListFragment.this.context, null);
                                    HttpParams httpParams = new HttpParams();
                                    httpParams.put("id", pItem.getOrderId());
                                    finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.CONFIRM_RECEIVE, httpParams, Url.CONFIRM_RECEIVE, new Callback<BaseResponse>() {
                                        @Override
                                        public void onSuccess(BaseResponse baseResponse) {
                                            dismissDialog();
                                            if (baseResponse.getResult().equals(Constant.REQUEST_SUCCESS)) {
//                                                quickAdapter.remove(helper.getPosition());
//                                                quickAdapter.notifyDataSetChanged();
//                                                onRefresh(refreshLayout);
                                                EventBus.getDefault().post("2");
                                                EventBus.getDefault().post("3");
                                            } else {
                                                showToast(baseResponse.getMsg());
                                            }
                                        }
                                    });
                                }
                            });
                            dialog_layout.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    DialogUtil.getInstance().dialog_center.dismiss();
                                }
                            });
                        }
                    });
                } else if (pItem.getStatus().equals("3")) {
                    TextView right_tv = helper.getView(R.id.right_tv);
                    TextView left_tv = helper.getView(R.id.left_tv);
                    right_tv.setText("评价");
                    right_tv.setVisibility(View.VISIBLE);
                    left_tv.setVisibility(View.GONE);
                    QuickAdapter<OrderListBean.MapBean.PageInfoBean.ListBean.DetailListBean> child_adapter =
                            new QuickAdapter<OrderListBean.MapBean.PageInfoBean.ListBean.DetailListBean>(OrderListFragment.this.context, R.layout.item_child_order_list, pItem.getDetailList()) {
                                @Override
                                protected void convert(BaseAdapterHelper helper, final OrderListBean.MapBean.PageInfoBean.ListBean.DetailListBean item) {
                                    helper.getView(R.id.item_rl).setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            Bundle bundle = new Bundle();
                                            bundle.putInt("id", item.getMerchandiseId());
                                            startActivity(ShopDetailActivity.class, bundle);
                                        }
                                    });
                                    TextView return_money_goods = helper.getView(R.id.return_money_goods);
                                    return_money_goods.setVisibility(View.VISIBLE);
                                    return_money_goods.setText("退货/退款");

                                    helper.setText(R.id.title, item.getMerchandiseName());
                                    helper.setText(R.id.money, "￥" + df.format(item.getSinglePrice() / 100));
                                    List<ShopDetailMcipChildBean> mcipList = GsonUtil.jsonToList(item.getMcipValue(), ShopDetailMcipChildBean.class);
                                    StringBuilder stringBuilder = new StringBuilder();
                                    for (int i = 0; i < mcipList.size(); i++) {
                                        if (i == mcipList.size() - 1) {
                                            stringBuilder.append(mcipList.get(i).getPropertyValue());
                                        } else {
                                            stringBuilder.append(mcipList.get(i).getPropertyValue() + ";");
                                        }
                                    }
                                    helper.setText(R.id.num_spec, "数量：x" + item.getPurchaseAmount() + ";" + stringBuilder.toString());
                                    RCRelativeLayout rc_layout = helper.getView(R.id.rc_layout);
                                    ImageView photo = helper.getView(R.id.photo);
                                    Glide.with(OrderListFragment.this.context)
                                            .load(item.getPhotoUrl())
                                            .error(R.mipmap.default_small_bg)
                                            .into(photo);
                                    rc_layout.setTopLeftRadius(6);
                                    rc_layout.setTopRightRadius(6);
                                    rc_layout.setBottomLeftRadius(6);
                                    rc_layout.setBottomRightRadius(6);

                                    return_money_goods.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            Bundle bundle = new Bundle();
                                            bundle.putString("tag", "退货/退款");
                                            bundle.putInt("orderId", pItem.getOrderId());
                                            bundle.putSerializable("DetailListBean", item);
                                            startActivity(RefundServiceActivity.class, bundle);
                                        }
                                    });
                                }
                            };
                    child_listview.setAdapter(child_adapter);

                    right_tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("listBean", pItem);
                            startActivity(CommentActivity.class, bundle);
                        }
                    });
                } else if (pItem.getStatus().equals("6")) {
                    TextView right_tv = helper.getView(R.id.right_tv);
                    TextView left_tv = helper.getView(R.id.left_tv);
                    right_tv.setVisibility(View.GONE);
                    left_tv.setVisibility(View.VISIBLE);
                    left_tv.setText("交易关闭");
                    QuickAdapter<OrderListBean.MapBean.PageInfoBean.ListBean.DetailListBean> child_adapter =
                            new QuickAdapter<OrderListBean.MapBean.PageInfoBean.ListBean.DetailListBean>(OrderListFragment.this.context, R.layout.item_child_order_list, pItem.getDetailList()) {
                                @Override
                                protected void convert(BaseAdapterHelper helper, final OrderListBean.MapBean.PageInfoBean.ListBean.DetailListBean item) {
                                    helper.getView(R.id.item_rl).setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            Bundle bundle = new Bundle();
                                            bundle.putInt("id", item.getMerchandiseId());
                                            startActivity(ShopDetailActivity.class, bundle);
                                        }
                                    });
                                    helper.setText(R.id.title, item.getMerchandiseName());
                                    helper.setText(R.id.money, "￥" + df.format(item.getSinglePrice() / 100));
                                    List<ShopDetailMcipChildBean> mcipList = GsonUtil.jsonToList(item.getMcipValue(), ShopDetailMcipChildBean.class);
                                    StringBuilder stringBuilder = new StringBuilder();
                                    for (int i = 0; i < mcipList.size(); i++) {
                                        if (i == mcipList.size() - 1) {
                                            stringBuilder.append(mcipList.get(i).getPropertyValue());
                                        } else {
                                            stringBuilder.append(mcipList.get(i).getPropertyValue() + ";");
                                        }
                                    }
                                    helper.setText(R.id.num_spec, "数量：x" + item.getPurchaseAmount() + ";" + stringBuilder.toString());
                                    RCRelativeLayout rc_layout = helper.getView(R.id.rc_layout);
                                    ImageView photo = helper.getView(R.id.photo);
                                    Glide.with(OrderListFragment.this.context)
                                            .load(item.getPhotoUrl())
                                            .error(R.mipmap.default_small_bg)
                                            .into(photo);
                                    rc_layout.setTopLeftRadius(6);
                                    rc_layout.setTopRightRadius(6);
                                    rc_layout.setBottomLeftRadius(6);
                                    rc_layout.setBottomRightRadius(6);

                                }
                            };
                    child_listview.setAdapter(child_adapter);

                    left_tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                        }
                    });
                }
            }
        };

        listView.setAdapter(quickAdapter);

        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void OnMessageEvent(String s) {
        if (s.equals("updateOrderList")) {
            pageNum = 1;
            initData();
        } else {
            status = s;
            switch (s) {
                case "0":
                    pageNum = 1;
                    initData();
                    break;
                case "1":
                    pageNum = 1;
                    initData();
                    break;
                case "2":
                    pageNum = 1;
                    initData();
                    break;
                case "3":
                    pageNum = 1;
                    initData();
                    break;
            }
        }
        status = "";
        pageNum = 1;
        initData();
    }


    @Override
    protected void initData() {
//        showDialog(context, null);
        HttpParams httpParams = new HttpParams();
        httpParams.put("customerId", tokenBean.getId());
        httpParams.put("status", status);
        httpParams.put("pageNum", pageNum++);
        httpParams.put("pageSize", pageSize);
        finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.ORDER_LIST, httpParams, Url.ORDER_LIST, new Callback<OrderListBean>() {
            @Override
            public void onSuccess(OrderListBean orderListBean) {
                if (orderListBean.getResult().equals(Constant.REQUEST_SUCCESS) && orderListBean.getMap() != null && orderListBean.getMap().getPageInfo().getList() != null && orderListBean.getMap().getPageInfo().getList().size() != 0) {
                    refreshLayout.setNoMoreData(false);
                    if (pageNum - 1 == 1) {
                        quickAdapter.clear();
                        for (int i = 0; i < orderListBean.getMap().getPageInfo().getList().size(); i++) {
                            if (orderListBean.getMap().getPageInfo().getList().get(i).getStatus().equals("0")
                                    || orderListBean.getMap().getPageInfo().getList().get(i).getStatus().equals("1")
                                    || orderListBean.getMap().getPageInfo().getList().get(i).getStatus().equals("2")
                                    || orderListBean.getMap().getPageInfo().getList().get(i).getStatus().equals("3")
                                    || orderListBean.getMap().getPageInfo().getList().get(i).getStatus().equals("6")
                                    || orderListBean.getMap().getPageInfo().getList().get(i).getStatus().equals("7")) {
                                quickAdapter.add(orderListBean.getMap().getPageInfo().getList().get(i));
                            }
                        }
                    } else {
                        for (int i = 0; i < orderListBean.getMap().getPageInfo().getList().size(); i++) {
                            if (orderListBean.getMap().getPageInfo().getList().get(i).getStatus().equals("0")
                                    || orderListBean.getMap().getPageInfo().getList().get(i).getStatus().equals("1")
                                    || orderListBean.getMap().getPageInfo().getList().get(i).getStatus().equals("2")
                                    || orderListBean.getMap().getPageInfo().getList().get(i).getStatus().equals("3")
                                    || orderListBean.getMap().getPageInfo().getList().get(i).getStatus().equals("6")
                                    || orderListBean.getMap().getPageInfo().getList().get(i).getStatus().equals("7")) {
                                quickAdapter.add(orderListBean.getMap().getPageInfo().getList().get(i));
                            }
                        }
                    }
                } else {
                    if (pageNum - 1 == 1) {
                        quickAdapter.clear();
                    }
                    refreshLayout.finishLoadMoreWithNoMoreData();
                }

                refreshLayout.finishRefresh();
                refreshLayout.finishLoadMore();
                quickAdapter.notifyDataSetChanged();

//                dismissDialog();
            }
        });
    }

    @Override
    @OnClick({})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {

        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        initData();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageNum = 1;
        initData();
    }

    @Override
    public void ZFBSuccess(String code) {
        dismissDialog();
        HttpParams httpParams = new HttpParams();
        httpParams.put("customerId", tokenBean.getId());
        httpParams.put("orderId", orderId);
        httpParams.put("payMode", Constant.NUM_1 + "");
        finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.SHOP_NOTIFY_PAY, httpParams, Url.SHOP_NOTIFY_PAY, new Callback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse baseResponse) {
                if (baseResponse.getResult().equals(Constant.REQUEST_SUCCESS)) {
//                    onRefresh(refreshLayout);
                    EventBus.getDefault().post("0");
                    EventBus.getDefault().post("1");
                    showPaySuccess();
                } else {
                    showFaileDialog();
                }
            }

            @Override
            public void onError(Response response) {
                super.onError(response);
                showFaileDialog();
            }
        });
    }

    @Override
    public void ZFBFail(String code) {
        dismissDialog();
    }

    @Override
    public void ZFBResultQueRen(String code) {

    }

    @Override
    public void callBack(BaseResp baseResp) {
        dismissDialog();
        if(baseResp.errCode != BaseResp.ErrCode.ERR_OK){
            return;
        }
        HttpParams httpParams = new HttpParams();
        httpParams.put("customerId", tokenBean.getId());
        httpParams.put("orderId", orderId);
        httpParams.put("payMode", Constant.NUM_2 + "");
        finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.SHOP_NOTIFY_PAY, httpParams, Url.SHOP_NOTIFY_PAY, new Callback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse baseResponse) {
                if (baseResponse.getResult().equals(Constant.REQUEST_SUCCESS)) {
//                    onRefresh(refreshLayout);
                    EventBus.getDefault().post("0");
                    EventBus.getDefault().post("1");
                    showPaySuccess();
                } else {
                    showFaileDialog();
                }
            }

            @Override
            public void onError(Response response) {
                super.onError(response);
                showFaileDialog();
            }
        });
    }

    private void showPaySuccess() {
        DialogUtil.getInstance().showCenterDialog(context, R.layout.dialog_shop_pay_success);
        TextView message = DialogUtil.getInstance().dialog_center_layout.findViewById(R.id.message);
        message.setText("");
        message.setVisibility(View.INVISIBLE);
        DialogUtil.getInstance().dialog_center_layout.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.getInstance().dialog_center.dismiss();
            }
        });
    }

    private void showFaileDialog() {
        LayoutInflater mInflater = LayoutInflater.from(context);
        LinearLayout dialog_pay_faile = (LinearLayout) mInflater.inflate(R.layout.dialog_pay_faile, null);
        Dialog dialog = new AlertDialog.Builder(context, R.style.NoBackGroundDialog).create();
        dialog.show();
        dialog.setContentView(dialog_pay_faile);
    }
}
